package com.lv.service.room.category

import android.content.Context
import android.os.Parcelable
import androidx.annotation.Keep
import androidx.lifecycle.LiveData
import androidx.room.*
import com.github.gzuliyujiang.wheelview.contract.TextProvider
import com.lv.service.bean.TelSecondCategory
import com.lv.service.room.helper.InspectionCategoryScoundConverter
import kotlinx.parcelize.Parcelize

/**
 * 作者： ygx
 * 创建日期：2022/1/8 2:42 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * App的公共业务基础数据类
 */

//3、database
@Database(entities = [SpuCategoryBean::class], version = 1, exportSchema = false)
abstract class SpuCategoryDatabase : RoomDatabase() {

    abstract val categoryDao: SpuCategoryDao

    companion object {

        private const val USER_DB_NAME = "syy_o_spu_category_db"

        @Volatile
        private var instance: SpuCategoryDatabase? = null

        @Synchronized
        fun getInstance(context: Context): SpuCategoryDatabase {
            return instance ?: Room.databaseBuilder(
                context,
                SpuCategoryDatabase::class.java,
                USER_DB_NAME
            ).build().also { instance = it }
        }
    }
}

//1、entity的定义
@TypeConverters(InspectionCategoryScoundConverter::class)
@Parcelize
@Entity(tableName = "category_db")
@Keep
data class SpuCategoryBean(
    @PrimaryKey
    val _id: Int,//主键
    val fcaid: Int,
    val id: String,
    val image: String,
    val name: String,
    val seq: Int,
    val telSecondCategories: List<TelSecondCategory>?,
    var isSelcted: Boolean,
) : Parcelable, TextProvider {
    override fun provideText(): String {
        return name
    }

}

////1、entity的定义
//
//@Entity(tableName = "tb_cniao_user")
//data class CniaoUserInfo(
//    @PrimaryKey
//    val id: Int,//主键
//    val course_permission: Boolean,
//    val token: String?,
//    @Embedded
//    val user: User?
//) {
//    @Keep
//    data class User(
//        @ColumnInfo(name = "cniao_user_id")
//        val id: Int,//用户id
//        val logo_url: String?,//用户头像
//        val reg_time: String?,//用户注册时间
//        val username: String?//用户名
//    )
//}

//2、dao层的定义
@Dao
interface SpuCategoryDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertFindCategory(categoryBean: SpuCategoryBean)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertFindSuspendCategory(categoryBean: SpuCategoryBean)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertFindSuspendCategory(categoryBean: List<SpuCategoryBean>)

    @Update(onConflict = OnConflictStrategy.REPLACE)
    fun updateFindCategory(categoryBean: SpuCategoryBean)

    @Delete
    fun deleteFindCategory(categoryBean: SpuCategoryBean)

    @Query("delete from category_db")
    fun deleteAllCategory()

    @Query("delete from category_db")
    suspend fun deleteSuspendAllCategory()

    // = , in , like
    @Query("select * from category_db where _id=:id")
    fun queryLiveFindCategory(id: Int = 0): LiveData<SpuCategoryBean>

    @Query("select * from category_db")
    fun queryLiveFindAllCategory(): LiveData<List<SpuCategoryBean>>

//    @Query("select * from category_db where _id=:id")
//    fun queryFindCategory(id: Int = 0): CategoryBean?

//    @Query("select * from category_db")
//    fun queryFindAllCategory(): List<CategoryBean>?

    @Query("select * from category_db where _id =:id")
    suspend fun getFindCategory(id: Int = 0): SpuCategoryBean?

    @Query("select * from category_db")
    suspend fun getAllCategory(): List<SpuCategoryBean>?
}



